<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\ChartModel;
use cmf\controller\AdminBaseController;
use think\Request;


class ChartController extends AdminBaseController
{
    //滚动图显示页面
    public function index(Request $request)
    {
        $selkey = $request->get('selkey');
        $where = [];
        if($selkey)
        {
            $where[]=['title','like','%'.$selkey.'%'];
        }
        $res = ChartModel::order('id','desc')
            ->where($where)
            ->paginate(2);
        $this->assign('list',$res);
        return $this->fetch();
    }
    //滚动图添加页面
    public function add()
    {
        return $this->fetch();
    }
    //添加提交
    public function addPost()
    {
        $image = $_POST['image'];
        $image = cmf_get_image_url($image);
        $res = ChartModel::create([
            'url'=>'#',
            'image'=>$image
        ]);
        if($res){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }
    //修改页面
    public function edit(Request $request)
    {
        $id = $request->param('id');
        $res = ChartModel::where('id','=',$id)
            ->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    //删除
    public function delete(Request $request)
    {
        $id = $request->param('id/d');
        $res = ChartModel::where('id',$id)
            ->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //是否展示
    public function show(Request $request)
    {
        $id = $request->param('id/d');
        $res = ChartModel::where('id',$id)->find();

        if($res['switch'] == 1){
            $res['switch'] = 0;
        }else{
            $res['switch'] = 1;
        }
        $list = ChartModel::where('id','=',$id)
            ->update([
                'switch'=>$res['switch']
            ]);
        if($list){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
}